package cn.ffcs.uec.corp.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uac.staff.model.TmpSkHis;
import cn.ffcs.uec.corp.dao.UecPsnDao;
import cn.ffcs.uec.corp.model.UecPsnCorp;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecPsnDao")
@Transactional
public class UecPsnDaoImpl extends BaseDaoImpl implements UecPsnDao {

	@Override
	public PageInfo queryPageInfoByUecPsnCorp(UecPsnCorp uecPsnCorp, int current, int pageSize) {
		if (null != uecPsnCorp) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSN_CORP T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (!StrUtil.isNullOrEmpty(uecPsnCorp.getPkCorp())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_CORP = ? ");
				params.add(uecPsnCorp.getPkCorp());
			} else if (!StrUtil.isNullOrEmpty(uecPsnCorp.getPsnCorpId())) {
				sb.append(" AND T.PSN_CORP_ID = ? ");
				params.add(uecPsnCorp.getPsnCorpId());
			}
			if (!StrUtil.isNullOrEmpty(uecPsnCorp.getUnitcode())) {
				sb.append(" AND T.UNITCODE LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(uecPsnCorp.getUnitcode()) + "%");
			}
			if (!StrUtil.isNullOrEmpty(uecPsnCorp.getUnitname())) {
				sb.append(" AND T.UNITNAME LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(uecPsnCorp.getUnitname()) + "%");
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecPsnCorp.class);
		}
		return null;
	}

	@Override
	public List<TmpSkHis> queryKeyIDList() {

		StringBuilder sb = new StringBuilder(" select distinct key_id keyId from tmp_sk_his ");
		List<Object> params = new ArrayList<Object>();
		sb.append(" where 1 = ? ");
		params.add("1");
		List<TmpSkHis> list = super.jdbcFindList(sb.toString(), params, TmpSkHis.class);
		if (list != null) {
			return list;
		}
		return null;
	}

	// public String getSeqEcode() {
	// String sql = "SELECT SEQ_STAFF_CODE.NEXTVAL FROM DUAL";
	// return String.valueOf(getJdbcTemplate().queryForInt(sql));
	// }

	@Override
	public List<TmpSkHis> queryTmpSkHisList(TmpSkHis tmpSkHis) {
		StringBuilder sb = new StringBuilder(
				" select key_id keyId,begin_Date beginDate,end_Date endDate,name name,flag flag from tmp_sk_his ");
		List<Object> params = new ArrayList<Object>();
		if (tmpSkHis != null) {
			if (!StrUtil.isNullOrEmpty(tmpSkHis.getKeyID())) {
				sb.append(" where key_id = ? ");
				params.add(tmpSkHis.getKeyID());
			}
		}
		List<TmpSkHis> list = super.jdbcFindList(sb.toString(), params, TmpSkHis.class);
		if (list != null) {
			return list;
		}
		return null;
	}

	@Override
	public int updateTmpSkHis(String keyId, String message) {
		if (!"".equals(keyId)) {
			String sql = " update tmp_sk_his set flag = ? where key_id = ?";
			int flag = this.getJdbcTemplate().update(sql, new Object[]{message,keyId});
			return flag;
		}
		return 0;
	}
	/**
	 * 删除员工时删除人力中间表 已过期
	 * 
	 * String sql =
	 * "UPDATE OPERATE_HR_TABLE_01 SET CURRENT_STATUS=? WHERE STAFF_ID=?";
	 * List params = new ArrayList();
	 * params.add(OperateHrConstant.STATUS_DEL); params.add(LStaffId);
	 * this.executeUpdateByJdbcAndParams(sql, params);
	 */
	
	
}
